/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package meteocal.control;

import java.util.Calendar;
import java.util.List;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.inject.Inject;
import meteocal.entity.AlternativeNotification;
import meteocal.entity.Event;
import meteocal.entity.InviteNotification;
import meteocal.entity.Notification;
import meteocal.entity.RescheduleNotification;
import meteocal.entity.Users;
import meteocal.entity.WeatherNotification;
import meteocal.entityManager.NotificationManager;

/**
 *
 * @author andrea
 */
@Stateless
public class NotificationControl {
    
    @Inject
    NotificationManager nm;
    
    /**
     * Sends the notification to every user in the list.
     * @param notification
     * @param users 
     */
    public void sendNotification(Notification notification, List<Users> users){
        if(notification instanceof AlternativeNotification){
            AlternativeNotification not = (AlternativeNotification) notification;
            for(Users user : users){
                AlternativeNotification tmp = new AlternativeNotification();
                tmp.setAlternative(not.getAlternative());
                tmp.setBadWeather(not.getBadWeather());
                tmp.setEvent(not.getEvent());
                tmp.setUser(user);
                tmp.setSeen(false);
                nm.save(tmp);
            }
        }
        if(notification instanceof InviteNotification){
        InviteNotification not = (InviteNotification) notification;
            for(Users user : users){
                InviteNotification tmp = new InviteNotification();
                tmp.setOrganizer(not.getOrganizer());
                tmp.setEvent(not.getEvent());
                tmp.setUser(user);
                tmp.setSeen(false);
                nm.save(tmp);
            }
        }
        if(notification instanceof RescheduleNotification){
        RescheduleNotification not = (RescheduleNotification) notification;
            for(Users user : users){
                RescheduleNotification tmp = new RescheduleNotification();
                tmp.setNewDate(not.getNewDate());
                tmp.setNewPlace(not.getNewPlace());
                tmp.setNewTitle(not.getNewTitle());
                tmp.setEvent(not.getEvent());
                tmp.setUser(user);
                tmp.setSeen(false);
                nm.save(tmp);
            }
        }
        if(notification instanceof WeatherNotification){
        WeatherNotification not = (WeatherNotification) notification;
            for(Users user : users){
                WeatherNotification tmp = new WeatherNotification();
                tmp.setBadWeather(not.getBadWeather());
                tmp.setEvent(not.getEvent());
                tmp.setUser(user);
                tmp.setSeen(false);
                nm.save(tmp);
            }
        }
    }
    
    /**
     * Removes the user's notifications related to the given event.
     * @param event
     * @param user 
     */
    public void removeNotification(Event event, Users user){
        List<Notification> nots = nm.findByEventAndUser(event, user);
        for(Notification n : nots){
            nm.delete(n);
        }
    }
    
    /**
     * Periodically removes notifications from the database if the related event
     * is past. Timer fires everyday at midnight.
     */
    @Schedule(persistent = false)
    private void removeNotification(){
        List<Notification> nots = nm.findAll();
        Calendar today = Calendar.getInstance();
        if(nots != null){
            for(Notification n : nots){
                if(n.getEvent().getEndTime().before(today))
                    nm.delete(n);
            }
        }
    }
    
}
